package controllers;

import models.cards.IPlayerCard;
import models.cards.PlayerCityCard;
import models.city.City;
import models.desease.Disease;
import models.game.Game;
import models.players.Player;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

@Controller
public class MapController {

	@RequestMapping("/MapHandler")
	public synchronized String updateMap(HttpServletRequest request) {
		
		Game game = (Game) request.getSession().getAttribute("game");
		
		//adds the log to the session
		ArrayList<String> log = new ArrayList<String>();
		log.add("------------------------");
		log.add("Player cards left: " + game.getPlayerDeck().getDeck().size());
		log.add("Outbreaks: " + game.getOutbreaks());
		for (Disease disease : game.getDiseases().values()) {
			if (disease.getIsCured() == true) {
				log.add(disease.getName() + " disease is cured!");				
			}
			if (disease.getIsEradicated() == true) {
				log.add(disease.getName() + " disease is eradicated");
			}
		}
		log.add("------------------------");
		
		log.addAll(game.getLog());
		request.getSession().setAttribute("log", log);
		
		//adds the players location to the session
		HashMap<String, HashSet<String>> playersCardsList = new HashMap<String, HashSet<String>>();
		ArrayList<String> playerUsernames = new ArrayList<String>();
		
		Integer i = 0;
		for(Player player : game.getPlayers().values()) {
			request.getSession().setAttribute(("top_p" + i), player.getLocation().getTopDistanse());
			request.getSession().setAttribute(("left_p" + i), player.getLocation().getLeftDistanse());
			
			HashSet<String> handCardInfo = new HashSet<String>();
			for (IPlayerCard card : player.getHand().values()) {
				if (card instanceof PlayerCityCard) {
					String color = ((PlayerCityCard) card).getDisease().getColor();
					String name = ((PlayerCityCard) card).getCity().getName();
					handCardInfo.add(color + " : " + name);
				}
			}
			
			playersCardsList.put(player.getUsername(), handCardInfo);
			
			i = i + 1;
			playerUsernames.add(player.getUsername());
		}
		request.getSession().setAttribute("playerUsernames", playerUsernames);
		request.getSession().setAttribute("numberOfPlayers", i);
		request.getSession().setAttribute("playersCardsList", playersCardsList);
		
		//adds cities info to the session
		ArrayList<Integer> topDistance = new ArrayList<Integer>();
		ArrayList<Integer> leftDistance = new ArrayList<Integer>();
		ArrayList<Integer> infectionLevel = new ArrayList<Integer>();
		ArrayList<String> diseaseColor = new ArrayList<String>();
		ArrayList<Boolean> hasRC = new ArrayList<Boolean>();
		
		for(City city : game.getCities().values()) {
			topDistance.add(city.getTopDistanse());
			leftDistance.add(city.getLeftDistanse());
			infectionLevel.add(city.getInfectionLevel());
			diseaseColor.add(city.getDisease().getColor());
			hasRC.add(city.getHasResearchCentre());
		}
		
		request.getSession().setAttribute("topDistance", topDistance);
		request.getSession().setAttribute("leftDistance", leftDistance);
		request.getSession().setAttribute("infectionLevel", infectionLevel);
		request.getSession().setAttribute("diseaseColor", diseaseColor);
		request.getSession().setAttribute("hasRC", hasRC);
		
		return "redirect:/ContinuingGameRedirect";
	}
	
}
